package com.sunyard.manage.controller;

import com.sunyard.constant.ErrMessageConst;
import com.sunyard.constant.manage.CommonConst;
import com.sunyard.dal.bo.operator.HandLoginBO;
import com.sunyard.dal.bo.device.lock.LoginTicketBO;
import com.sunyard.dal.bo.operator.OperatorLoginBO;
import com.sunyard.dal.bo.operator.OperatorUpdatePwdBO;
import com.sunyard.dal.dto.LoginBackDTO;
import com.sunyard.manage.service.OperatorService;
import com.sunyard.utils.RandomValidateCode;
import com.sunyard.utils.Response.BaseResult;
import com.sunyard.utils.Response.GetResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * @author jiy.fang
 * @Description 登录控制器
 * @date 2019/7/9
 */

@Api(tags = "LoginController", description = "用户登录控制器")
@Slf4j
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private OperatorService operatorService;

    /**
     * @Description: 用户登录
     * @Author: jiy.fang
     * @Date: 2019/7/16 20:03
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("用户登录")
    public BaseResult<LoginBackDTO> login(HttpServletResponse response, HttpServletRequest request, @Valid @RequestBody OperatorLoginBO operatorLoginBO) {
        try {
            return operatorService.logIn(response, request, operatorLoginBO);
        } catch (Exception e) {
            log.error(ErrMessageConst.SYSTEM_ERROR, e);
            return GetResult.getResultErr();
        }
    }

    /**
     * @Description: 门户登录
     * @Author: tsz
     * @Date: 2019/7/16 20:03
     */
    @RequestMapping(value = "/doorLogin", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("门户登录")
    public BaseResult<LoginBackDTO> login(HttpServletResponse response, HttpServletRequest request, @Valid @RequestBody LoginTicketBO loginTicketBO) {
        try {
            return operatorService.logInById(response, request, loginTicketBO.getTicket());
        } catch (Exception e) {
            log.error(ErrMessageConst.SYSTEM_ERROR, e);
            return GetResult.getResultErr();
        }
    }

    /**
     * @Description: 用户登录
     * @Author: jiy.fang
     * @Date: 2019/7/16 20:03
     */
    @RequestMapping(value = "/handLogin", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("指纹登录")
    public BaseResult<LoginBackDTO> handLogin(HttpServletResponse response, HttpServletRequest request, @Valid @RequestBody HandLoginBO handLoginBO) {
        try {
            return operatorService.handLogin(response, request, handLoginBO);
        } catch (Exception e) {
            log.error(ErrMessageConst.SYSTEM_ERROR, e);
            return GetResult.getResultErr();
        }
    }

    /**
     * @Description: 用户登出
     * @Author: jiy.fang
     * @Date: 2019/7/17 10:45
     */
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("用户登出")
    public BaseResult logOut(HttpServletRequest request) {
        try {
            return operatorService.logOut(request);
        } catch (Exception e) {
            log.error(ErrMessageConst.SYSTEM_ERROR, e);
            return GetResult.getResultErr();
        }
    }

    /**
     * @Description: 修改密码
     * @Author: jiy.fang
     * @Date: 2019/7/17 13:45
     */
    @RequestMapping(value = "/updatePwd", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("修改密码")
    public BaseResult updatePwd(HttpServletRequest request, @Valid @RequestBody OperatorUpdatePwdBO operatorUpdatePwdBO) {
        try {
            return operatorService.updatePwd(request, operatorUpdatePwdBO);
        } catch (Exception e) {
            log.error(ErrMessageConst.SYSTEM_ERROR, e);
            return GetResult.getResultErr();
        }
    }

    /**
     * @Description: 浏览器是否已经登录一个账号
     * @Author: jiy.fang
     * @Date: 2019/9/6 13:52
     */
    @RequestMapping(value = "/checkLogin", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("判断浏览器是否已经登录一个账号")
    public String checkLogin(HttpSession session, HttpServletRequest request) {
        try {
            return operatorService.checklogin(request);
        } catch (Exception e) {
            log.error(ErrMessageConst.SYSTEM_ERROR, e);
            return CommonConst.JSON_SYS_ERROR;
        }
    }

    @RequestMapping(value = "/getOperatorInfo", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("实时获取登录用户信息")
    public BaseResult<LoginBackDTO> getOperatorInfo(HttpServletRequest request) {
        try {
            return operatorService.getOperatorInfo(request);
        } catch (Exception e) {
            log.error(ErrMessageConst.SYSTEM_ERROR, e);
            return GetResult.getResultErr();
        }
    }

    /**
     * 登录页面生成随机验证码
     */
    @RequestMapping(value = "/getVerify", method = RequestMethod.POST)
    public void getVerify(HttpServletRequest request, HttpServletResponse response) {
        //设置相应类型,告诉浏览器输出的内容为图片
        response.setContentType("image/jpeg");
        //设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        RandomValidateCode randomValidateCode = new RandomValidateCode();
        try {
            //输出验证码图片方法
            randomValidateCode.getRandcode(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
